package com.voxel.sense.infomsg.module.user.personal.portrait;

import com.example.com.common.app.BasePresenter;
import com.example.com.common.factory.data.DataSource;
import com.example.com.common.utils.BitmapUtils;
import com.example.com.common.utils.FileUtils;
import com.example.com.common.utils.L;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.utils.Utils;
import com.example.com.common.utils.bean.Size;
import com.voxel.sense.infomsg.net.helper.FileOperatorNetHelper;
import com.voxel.sense.infomsg.utils.AppFileUtil;

import java.io.File;

import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.share.SPEngine;

/**
 * Created by ChenYiYao on 2018/4/11.
 */

public class PortraitPresenter extends BasePresenter<PortraitContract.View>
        implements PortraitContract.Presenter, DataSource.Callback<BaseEntity<String>> {


    public PortraitPresenter(PortraitContract.View view) {
        super(view);
    }

    @Override
    public void doHandlerPhoto(File file) {
        if (file != null && file.exists()) {
            // L.d(""+paths.get(0));
            Size size = BitmapUtils.getImageSize(file.toString());
            if (size != null) {
                if (size.width < 500 || size.height < 500) {
                    upLoad(file);
                } else {
                    mView.onShowClipPhoto(file);
                }
            } else {
            }
        }
    }

    @Override
    public void doUploadPortrait(File file) {
        upLoad(file);
    }

    private void upLoad(File file){
        String fileExtension = FileUtils.getFileExtension(file);
        File uploadFile = new File(AppFileUtil.getImageDir(), Utils.getMD5(file.getName()) + "." + fileExtension);
        //压缩图片
        BitmapUtils.compressPicToFile(file, uploadFile, 500, 500, 1024 * 1024);
        L.d("FileSize:" + FileUtils.getFileSize(file) + "-----FileSize:" + FileUtils.getFileSize(uploadFile));
        //上传

        if (FileUtils.isFileExists(uploadFile)) {
            mView.showLoading("");
//            ProgressManager.getInstance().addRequestListener(UP_URl, getUploadListener());
            FileOperatorNetHelper.upPortrait(file, this);

        } else {
            ToastUtil.showToastLong(Factory.app(), "无法获取图片，上传失败");
        }
    }


    @Override
    public void onDataLoaded(BaseEntity<String> data, int reqId) {
        if(mView==null)
            return;

        mView.dimissLoading();
        if (data.isSuccess()) {
            String headImgURL = data.getData();
            SPEngine.getSPEngine().getUserInfo().setPortrait(headImgURL);
            mView.onUploadSuccess(headImgURL);
//            L.d("-----数据上传完成-----" + CommonConstants.FILE_URL_PREFIX_WEB + headImgURL);
        } else {

            mView.showError(data.getMessage());
        }
    }

    @Override
    public void onDataNotAvailable(String strRes, int reqId) {
        if(mView==null)
            return;

        mView.dimissLoading();
        mView.showError(strRes);
    }
}
